package org.wattdepot.cli.command;

import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.processor.CommandLineInterface;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * PowerGeneratedTime Class.
 * 
 * @author Remy Baumgarten
 * @author Kevin Chiogioji
 * 
 */
public class PowerGeneratedTime extends CommandLineInterface implements WattDepotCliCommand {

  /**
   * Command 2.7 - Prints information about power generated by a specified source.
   * 
   * @param args -
   * @return -
   */
  public String doCommand(List<String> args) {

    String type = args.get(0);
    String source = args.get(1);
    String timestamp = args.get(2);
    XMLGregorianCalendar time = null;
    Double powerData = null;
    try {
      time = Tstamp.makeTimestamp(timestamp);
      if (type.equalsIgnoreCase("generated")) {
        powerData = client.getPowerGenerated(source, time);
      }
      else if (type.equalsIgnoreCase(("consumed"))) {
        powerData = client.getPowerConsumed(source, time);
      }
      else {
        return "Invalid Command.";
      }
    }
    catch (MiscClientException mce) {
      return errorClient;
    }
    catch (BadXmlException bxe) {
      return errorXML;
    }
    catch (ResourceNotFoundException rnfe) {
      return error404;
    }
    catch (NotAuthorizedException nae) {
      return error401;
    }
    catch (Exception e) {
      return errorTimeStamp;
    }
    return powerData.toString();

  }
}
